package com.example.quartz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.admin.entity.Bidding;
import com.example.admin.service.IBiddingService;
import com.example.util.http.HttpUtils;
import com.example.util.sms.SmsUtils;
import com.example.util.str.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component("task")
public class Task {

    private String phone = "18084167573";
    private String templateId = "715186";
    private String findtxt = "停车";

    @Autowired
    private IBiddingService biddingService;

    //无参
    public void init() {
        System.out.println("无参数据");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<Bidding> list = getBidding(sdf.format(new Date()));
        if (list != null && list.size() > 0) {
            list.stream().filter(info -> {
                QueryWrapper queryWrapper = new QueryWrapper();
                queryWrapper.eq("title", info.getTitle());
                queryWrapper.eq("timeShow", sdf.format(new Date()));
                List<Bidding> bis = biddingService.list(queryWrapper);
                return !(bis != null && bis.size() > 0);
            }).forEach(bidding -> {
                bidding.setCreateTime(new Date());
                bidding.insert();
                SmsUtils.sendMsg(phone, templateId, new String[]{bidding.getTitle()});
            });
        }
    }

    public void init(String name) {

        System.out.println(String.format("参数:%s", name));
    }

    public void init(String name, String age) {

        System.out.println(String.format("参数:%s/%s", name, age));
    }


    private List<Bidding> getBidding(String date) {
        String url = "http://deal.ggzy.gov.cn/ds/deal/dealList_find.jsp";
        Map<String, String> params = new HashMap<>();
        params.put("TIMEBEGIN_SHOW", date);
        params.put("TIMEEND_SHOW", date);
        params.put("TIMEBEGIN", date);
        params.put("TIMEEND", date);
        params.put("SOURCE_TYPE", "1");
        params.put("DEAL_TIME", "01");
        params.put("DEAL_CLASSIFY", "00");
        params.put("DEAL_STAGE", "0000");
        params.put("DEAL_PROVINCE", "520000");
        params.put("DEAL_CITY", "0");
        params.put("DEAL_PLATFORM", "0");
        params.put("BID_PLATFORM", "0");
        params.put("DEAL_TRADE", "0");
        params.put("isShowAll", "1");
        params.put("PAGENUMBER", "1");
        params.put("FINDTXT", findtxt);
        String result = HttpUtils.sendPost(url, params);
        if (StringUtils.isNotEmpty(result)) {
            JSONObject json = JSONObject.parseObject(result);
            System.out.println(json.get("ttlrow"));
            if ("0".equals(json.get("ttlrow").toString())) {
                System.out.println("空");
            } else {
                System.out.println("今天有" + json.get("ttlrow"));
                JSONArray jsonArray = json.getJSONArray("data");
                return jsonArray.stream().map(info -> {
                    Bidding bidding = JSON.parseObject(info.toString(), Bidding.class);
                    return bidding;
                }).collect(Collectors.toList());
            }
        }
        System.out.println(result);
        return null;
    }

}
